﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Security.aspx.cs" Inherits="CRMWeb.Pages.AM.Security" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <input id="hdnType" type="hidden" />
    <div id="toolbarGroup">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newProperty()">New Property</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newVehicle()">New Vehicle</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newCompany()">New Company</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="editSecurity()">Edit</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="deleteSecurity()">Delete</a>
    </div>
    <table id="dgSecurity" class="easyui-datagrid" style="width: auto; height: 150px;" autorowheight="true"
        data-options="url: '/WebServices/AM/Security.ashx?method=GetSecurityForLoan&Loanid='+getQuerystring('LoanID', ''),pageSize:10"
        rownumbers="true" fitcolumns="true" singleselect="true" showfooter="true">
        <thead>
            <tr>
                <th field="id" hidden="true"></th>
                <th field="SecurityType" width="30">Security Type</th>
                <th field="Security" width="90">Security</th>
                <th field="Value" width="40">Value</th>
                <th field="Owning" width="40">Owning</th>
                <th field="Owner" width="90">Registered Owner</th>
                <th field="Status" width="40">Security Status</th>
            </tr>
        </thead>
    </table>
    <br />
    <div id="VehicleSecurityDetail" class="easyui-panel" style="width: auto; height: auto" hidden="true">
        <div class="ftitle">Vehicle Security Information</div>
        <table>
            <tr></tr>
            <tr>
                <td style="width: 300px">Year:&nbsp;&nbsp;&nbsp;
                    <label id="lblVehicleYear"></label>
                </td>
                <td style="width: 300px">Security Status :&nbsp;&nbsp;
                    <label id="lblVehicleStatus"></label>
                </td>
                <td style="width: 200px"></td>
                <td style="width: 200px" align="Center"><a href="#" class="easyui-linkbutton" style="width: 80px; margin-top: 5px" onclick="UpdateComment()">Save</a></td>
            </tr>
            <tr>
                <td>Make:&nbsp;&nbsp;&nbsp;
                    <label id="lblVehicleMake"></label>
                </td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td>Model:&nbsp;&nbsp;&nbsp;
                    <label id="lblVehicleModel"></label>
                </td>
                <td>Value:&nbsp;&nbsp;&nbsp;
                    <label id="lblVehicleValue"></label>
                    &nbsp;&nbsp;&nbsp;
                    Source:<label id="lblVehicleValueSource"> </label>
                </td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td>Badge:&nbsp;&nbsp;&nbsp;
                    <label id="lblVehicleBadge"></label>
                </td>
                <td>Owing:&nbsp;&nbsp;&nbsp;
                    <label id="lblVehicleOwning"></label>
                    &nbsp;&nbsp;&nbsp;
                    Source:<label id="lblVehicleOwningSource"></label>
                </td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td>Registered Owner:&nbsp;&nbsp;&nbsp;
                    <label id="lblVehicleOwner"></label>
                </td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td><b>Body :</b> </td>
                <td>Registration Expiry: &nbsp;&nbsp;
                    <label id="lblExpiryDate"></label>
                </td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td>Engine:&nbsp;&nbsp;&nbsp;
                    <label id="lblEngineNo"></label>
                </td>
                <td>Insurance Type : 
                    <label id="lblInsuranceType"></label>
                </td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td>Colour :&nbsp;&nbsp;&nbsp;
                    <label id="lblVehicleColour"></label>
                </td>
                <td>Comment: </td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td>Reg. No:&nbsp;&nbsp;&nbsp;
                    <label id="lblRegNo"></label>
                </td>
                <td rowspan="4">
                    <textarea id="lblVehicleComment" style="width: 300px; height: 50px"></textarea>
                </td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td>VIN:&nbsp;&nbsp;&nbsp;
                    <label id="lblVIN"></label>
                </td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </table>
    </div>
    <div id="PropertySecurityDetail" class="easyui-panel" style="width: auto; height: auto;" hidden="true">
        <div class="ftitle">Property Security Detail</div>
        <table>
            <tr>
                <td style="width: 400px">Address:
                    <label id="lblAddress"></label>
                </td>
                <td style="width: 150px">Value:
                    <label id="lblPropertyValue"></label>
                </td>
                <td style="width: 200px">Source:<label id="lblPropertyValueSource"></label></td>
                <td style="width: 200px" align="center"><a href="#" class="easyui-linkbutton" style="width: 81px; margin-top: 5px" onclick="UpdateComment()">Save</a></td>
            </tr>
            <tr>
                <td></td>
                <td style="width: 150px">Owning: 
                    <label id="lblPropertyOwning"></label>
                </td>
                <td style="width: 200px">Source: 
                    <label id="lblPropertyOwningSource"></label>
                </td>
                <td></td>
            </tr>
            <tr>
                <td>Title Ref.:
                    <label id="lblTitleRef"></label>
                </td>
                <td colspan="2"></td>
                <td></td>
            </tr>
            <tr>
                <td>Type:
                    <label id="lblPropertyType"></label>
                </td>
                <td colspan="2"></td>
                <td></td>
            </tr>
            <tr>
                <td>Owner:
                    <label id="lblPropertyOwner" />
                </td>
                <td colspan="2">1st Mortgagee :
                    <label id="lblMortgagee" />
                </td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td colspan="2">Subsequent Parties:
                    <label id="lblSubParties"></label>
                </td>
                <td></td>
            </tr>
            <tr>
                <td><b>Security Registration: </b></td>
                <td colspan="2"></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td colspan="2"></td>
                <td></td>
            </tr>
            <tr>
                <td>Security Status:
                    <label id="lblPropertyStatus"></label>
                </td>
                <td rowspan="4" colspan="2">Comment:
                    <br />
                    <textarea id="lblPropertyComment" style="width: 300px; height: 50px"></textarea>
                </td>
                <td></td>
            </tr>
            <tr>
                <td>Interest Type:
                    <label id="lblInterestType"></label>
                </td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td>Lodgement Date:<label id="lblLodgementDate"></label>
                </td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td>Completion Date:
                    <label id="lblCompletionDate"></label>
                </td>
                <td></td>
                <td></td>
            </tr>
            <tr></tr>
            <tr></tr>
        </table>
    </div>
    <%--Company Details--%>
    <div id="CompanySecurityDetail" class="easyui-panel" style="width: auto; height: auto;" hidden="true">
        <div class="ftitle">Company Security Detail</div>
        <table>
            <tr>
                <td style="width: 400px">Company:
                    <label id="lblCompany"></label>
                </td>
                <td style="width: 400px"></td>
                <td style="width: 200px" align="center"><a href="#" class="easyui-linkbutton" style="width: 81px; margin-top: 5px" onclick="UpdateComment()">Save</a></td>
            </tr>
            <tr>
                <td>Security:
                    <label id="lblCompanySecurity"></label>
                </td>
                <td style="width: 400px">Prior Chargee:
                    <label id="lblPriorChargee"></label>
                </td>
                <td style="width: 200px" align="center"></td>
            </tr>
            <tr>
                <td></td>
                <td>Subsequent Chargee:
                    <label id="lblSubsequentChargee"></label>
                </td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td>&nbsp;</td>
                <td></td>
            </tr>
            <tr>
                <td><b>Security Registration: </b></td>
                <td colspan="2" rowspan="4">Comment:
                    <br />
                    <textarea id="txtCompanyComment" style="width: 300px; height: 50px" cols="20" name="S1" rows="1"></textarea>
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>Security Status:
                    <label id="lblCompanySecurityStatus"></label>
                </td>
                <td></td>
            </tr>
            <tr>
                <td>Registration Date:
                    <label id="lblRegistrationDate"></label>
                </td>
                <td></td>
                <td></td>
            </tr>
        </table>
    </div>
    <%--Company Details end--%>
    <div id="dlgProperty" title="Property" class="easyui-dialog" style="width: 900px; height: auto; padding: 0px;" closed="true" center="true">
        <div id="pnlProperty" class="easyui-panel" style="width: auto; height: auto">
            <div class="ftitle">Property Security Detail</div>
            <div class="fitem">
                <table>
                    <tr>
                        <td style="width: auto;">
                            <label>Address:</label>
                        </td>
                        <td style="width: 350px; text-align: left;">
                            <input id="txtPropertyAddress" class="easyui-validatebox" style="width: 290px; background-color: LightGray;" disabled="disabled" />
                            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#dlgPropertyAddress').dialog('open')">...</a>
                        </td>
                        <td style="width: auto;">
                            <label>Title Reference :</label>
                        </td>
                        <td style="width: 300px; text-align: left;">
                            <input id="txtTitleRef" class="easyui-validatebox" style="width: 200px" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Property Type :</label>
                        </td>
                        <td>
                            <input id="txtPropertyType" class="easyui-validatebox" style="width: 200px" />
                        </td>

                        <td>
                            <label>Security Status:</label>
                        </td>
                        <td>
                            <select id="cmbPropertyStatus" class="easyui-combobox" style="width: 150px">
                                <option value="Pending">Pending</option>
                                <option value="Completed">Completed</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Owner Name :</label>
                        </td>
                        <td>
                            <input id="txtPropertyOwner" class="easyui-validatebox" style="width: 290px;background-color: LightGray;" disabled="disabled" />
                            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#dlgOwnerName').dialog('open')">...</a>
                        </td>
                        <td>
                            <label>Interest Type:</label>
                        </td>
                        <td>
                            <input id="txtInterestType" class="easyui-validatebox" style="width: 200px" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Lodgement Date: </label>
                        </td>
                        <td>
                            <input id="txtLodgementDate" class="easyui-datebox" data-options="formatter:formatDate" />
                        </td>
                        <td>
                            <label>Completion Date: </label>
                        </td>
                        <td>
                            <input id="txtCompletionDate" class="easyui-datebox" data-options="formatter:formatDate" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Property Value :</label>
                        </td>
                        <td>
                            <input id="txtPropertyValue" class="easyui-numberbox" style="width: 200px" />
                        </td>
                        <td>
                            <label>Value Source:</label>
                        </td>
                        <td>
                            <input id="txtPropertyValueSource" class="easyui-validatebox" style="width: 250px" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Property Owning :</label>
                        </td>
                        <td>
                            <input id="txtPropertyOwning" class="easyui-numberbox" style="width: 200px" />
                        </td>
                        <td>
                            <label>Owning Source:</label>
                        </td>
                        <td>
                            <input id="txtPropertyOwningSource" class="easyui-validatebox" style="width: 250px" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>1st Mortgagee :</label>
                        </td>
                        <td>
                            <input id="txtMortgagee" class="easyui-validatebox" style="width: 200px" />
                        </td>
                        <td>
                            <label>SubSequent Parties :</label>
                        </td>
                        <td>
                            <input id="txtSubParties" class="easyui-validatebox" style="width: 200px" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Comment :</label>
                        </td>
                        <td>
                            <input id="txtPropertyComment" class="easyui-validatebox" style="width: 200px" />
                        </td>
                        <td></td>
                        <td></td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="fitem" style="text-align: right">
            <input id="hdnPropertySecurityID" type="hidden" />
            <a href="#" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlgProperty').dialog('close')" style="float:left">Cancel</a>
            <a href="#" class="easyui-linkbutton" iconcls="icon-ok" onclick="SavePropertySecurity()">Save</a><br />
        </div>
    </div>
    <%--Address Dialog--%>
    <div id="dlgPropertyAddress" title="Property Address Information" class="easyui-dialog" style="width: 300px; height: 200px; padding: 10px" closed="true" buttons="#dlgPropertyAddress-buttons">
        <div class="fitem">
            <label style="width: 120px;">Street:</label>
            <input id="txtPropertyStreet" type="text" name="Street" class="easyui-validatebox" />
        </div>
        <div class="fitem">
            <label style="width: 120px;">Suburb:</label>
            <input id="txtPropertySuburb" type="text" name="Suburb" class="easyui-validatebox" />
        </div>
        <div class="fitem">
            <label style="width: 120px;">State:</label>
            <input id="txtPropertyState" name="State" type="text" class="easyui-validatebox" />
        </div>
        <div class="fitem">
            <label style="width: 120px;">Post Code:</label>
            <input id="txtPropertyPostCode" name="PostCode" type="text" class="easyui-validatebox" />
        </div>
        <div id="dlgPropertyAddress-buttons">
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="setPropertyAddress()">Save</a>
        </div>
    </div>
    <%--Name Dialog--%>
    <div id="dlgOwnerName" title="Owner Name Information" class="easyui-dialog" style="width: 300px; height: 200px; padding: 10px" closed="true" buttons="#dlgOwnerName-buttons">
        <div class="fitem">
            <label style="width: 120px;">First Name:</label>
            <input id="txtOwnerFirstName" type="text" name="FirstName" class="easyui-validatebox" />
        </div>
        <div class="fitem">
            <label style="width: 120px;">Middle Name:</label>
            <input id="txtOwnerMiddleName" type="text" name="MiddleName" class="easyui-validatebox" />
        </div>
        <div class="fitem">
            <label style="width: 120px;">Surname:</label>
            <input id="txtOwnerSurName" name="SurName" type="text" class="easyui-validatebox" />
        </div>
        <div id="dlgOwnerName-buttons">
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="setFullName()">Save</a>
        </div>
    </div>
    <div id="dlgVehicle" title="Vehicle" class="easyui-dialog" style="width: 1000px; height: auto; padding: 0px;" closed="true" center="true">
        <div id="pnlVehicle" class="easyui-panel" style="width: auto; height: auto">
            <div class="ftitle">Vehicle Security Information</div>
            <div class="fitem">
                <table>
                    <tr>
                        <td style="width: 160px;">
                            <label>Reg. No :</label>
                        </td>
                        <td style="width: 320px; text-align: left;">
                            <input id="txtVehicleRegNo" class="easyui-validatebox" style="width: 200px" />
                        </td>
                        <td style="width: 200px;">
                            <label>VIN</label>
                        </td>
                        <td style="width: 200px; text-align: left;">
                            <input id="txtVIN" class="easyui-validatebox" style="width: 200px" />
                        </td>
                        <td style="width: 100px;"></td>
                        <td style="width: 150px; text-align: left;"></td>
                    </tr>
                    <tr>
                        <td>
                            <label>Engine No.:</label>
                        </td>
                        <td>
                            <input id="txtEngineNo" class="easyui-validatebox" style="width: 200px" />
                        </td>
                        <td>
                            <label>Model</label>
                        </td>
                        <td>
                            <input id="txtVehicleModel" class="easyui-validatebox" style="width: 200px" />
                        </td>
                        <td>
                            <label>Colour :</label>
                        </td>
                        <td>
                            <input id="txtVehicleColour" class="easyui-validatebox" style="width: 100px" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Make :</label>
                        </td>
                        <td>
                            <input id="txtVehicleMake" class="easyui-validatebox" style="width: 200px" />
                        </td>
                        <td>
                            <label>Badge :</label>
                        </td>
                        <td>
                            <input id="txtVehicleBadge" class="easyui-validatebox" style="width: 200px" />
                        </td>
                        <td>
                            <label>Year :</label>
                        </td>
                        <td>
                            <input id="txtVehicleYear" class="easyui-numberbox" style="width: 100px" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Owner Name:</label>
                        </td>
                        <td>
                            <input id="txtVehicleOwner" class="easyui-validatebox" disabled="disabled" style="width: 200px;background-color: LightGray;" />
                            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#dlgVehicleOwnerName').dialog('open')">...</a>
                        </td>
                        <td>
                            <label>Security Status:</label>
                        </td>
                        <td>
                            <select id="cmbVehicleStatus" class="easyui-combobox" style="width: 150px">
                                <option value="Pending">Pending</option>
                                <option value="Completed">Completed</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Insurance Type: </label>
                        </td>
                        <td>
                            <input id="txtInsuranceType" class="easyui-validatebox" style="width: 250px" />
                        </td>
                        <td>
                            <label>Expiry Date: </label>
                        </td>
                        <td>
                            <input id="txtExpiryDate" class="easyui-datebox" data-options="formatter:formatDate" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Vehicle Value :</label>
                        </td>
                        <td>
                            <input id="txtVehicleValue" class="easyui-numberbox" style="width: 200px" />
                        </td>
                        <td>
                            <label>Value Source:</label>
                        </td>
                        <td>
                            <input id="txtVehicleValueSource" class="easyui-validatebox" style="width: 250px" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Vehicle Owning :</label>
                        </td>
                        <td>
                            <input id="txtVehicleOwning" class="easyui-numberbox" style="width: 200px" />
                        </td>
                        <td>
                            <label>Owning Source:</label>
                        </td>
                        <td>
                            <input id="txtVehicleOwningSource" class="easyui-validatebox" style="width: 250px" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Comment :</label>
                        </td>
                        <td>
                            <input id="txtVehicleComment" class="easyui-validatebox" style="width: 200px" />
                        </td>
                        <td></td>
                        <td></td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="fitem" style="text-align: right">
            <input id="hdnVehicleSecurityID" type="hidden" />
            <a href="#" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlgVehicle').dialog('close')" style="float:left">Cancel</a>
            <a href="#" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveVehicleSecuity()">Save</a><br />
        </div>
    </div>
    <%--Name Dialog--%>
    <div id="dlgVehicleOwnerName" title="Vehicle Owner Name Information" class="easyui-dialog" style="width: 300px; height: 200px; padding: 10px" closed="true" buttons="#dlgVehicleOwnerName-buttons">
        <div class="fitem">
            <label style="width: 120px;">First Name:</label>
            <input id="txtVehicleOwnerFirstName" type="text" name="FirstName" class="easyui-validatebox" />
        </div>
        <div class="fitem">
            <label style="width: 120px;">Middle Name:</label>
            <input id="txtVehicleOwnerMiddleName" type="text" name="MiddleName" class="easyui-validatebox" />
        </div>
        <div class="fitem">
            <label style="width: 120px;">Surname:</label>
            <input id="txtVehicleOwnerSurName" name="SurName" type="text" class="easyui-validatebox" />
        </div>
        <div id="dlgVehicleOwnerName-buttons">
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="setVehicleOwnerFullName()">Save</a>
        </div>
    </div>

    <%--dlgCompany--%>
    <div id="dlgCompany" title="Company" class="easyui-dialog" style="width: 830px; height: auto; padding: 0px;" closed="true" center="true">
        <div id="pnlCompany" class="easyui-panel" style="width: auto; height: auto">
            <div class="ftitle">Company Security Detail</div>
            <div class="fitem">
                <table>
                    <tr>
                        <td style="width: auto;">
                            <label>Company :</label>
                        </td>
                        <td style="width: 350px; text-align: left;">
                            <input id="txtCompany" class="easyui-validatebox" style="width: 290px" />
                        </td>

                        <td>
                            <label>Prior Chargee :</label>
                        </td>
                        <td>
                            <input id="txtPriorChargee" class="easyui-numberbox" style="width: 200px" />
                        </td>
                    </tr>
                    <tr>
                        <td style="width: auto;">
                            <label>Security :</label>
                        </td>
                        <td style="width: 300px; text-align: left;">
                            <input id="txtSecurity" class="easyui-validatebox" style="width: 200px" />
                        </td>
                        <td>
                            <label>Subsequent Chargee :</label>
                        </td>
                        <td>
                            <input id="txtSubsequentChargee" class="easyui-numberbox" style="width: 200px" />
                        </td>

                    </tr>
                    <tr>
                        <td>
                            <label>Registration Date :</label>
                        </td>
                        <td>
                            <input id="txtRegistrationDate" class="easyui-datebox" data-options="formatter:formatDate" />
                        </td>
                        <td rowspan="3">
                            <label>Comment :</label>
                        </td>
                        <td rowspan="3">
                            <textarea id="txtComment" class="easyui-validatebox" style="width: 200px; height: 50px;" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Security Status :</label>
                        </td>
                        <td>
                            <select id="txtSecurityStatus" class="easyui-combobox" style="width: 150px">
                                <option value="Pending">Pending</option>
                                <option value="Completed">Completed</option>
                            </select>
                        </td>
                    </tr>

                </table>
            </div>
        </div>
        <div class="fitem" style="text-align: right">
            <input id="hdnCompanySecurityID" type="hidden" />
            <a href="#" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlgCompany').dialog('close')" style="float:left">Cancel</a>
            <a href="#" class="easyui-linkbutton" iconcls="icon-ok" onclick="SaveCompanySecurity()">Save</a><br />
        </div>
    </div>
    <script type="text/javascript">
        function getTotalSecurity() {
            $.ajax({
                url: '/WebServices/AM/Security.ashx?method=GetTotalSecurity',
                contentType: "application/json; charset=utf-8",
                data: { LoanID: getQuerystring('LoanID', '') },
                success: function (count) {
                    alert(count);
                    countTotalSecurity(count);
                }
            });
        };

        $('#dgSecurity').datagrid({
            onClickRow: function (index, rowdata) {
                showDetail();
            }
        });
        setHeight();
        fnLeftNavSelected('A9');

        //get querystring value
        function getQuerystring(key, defaultValue) {
            if (defaultValue == null) defaultValue = "";
            key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
            var qs = regex.exec(window.location.href);
            if (qs == null)
                return defaultValue;
            else {
                return qs[1];
            }
        }

        function setFullName() {
            $('#txtPropertyOwner').val($('#txtOwnerFirstName').val() + ' ' + $('#txtOwnerMiddleName').val() + ' ' + $('#txtOwnerSurName').val());
            $('#dlgOwnerName').dialog('close');
        }

        function setPropertyAddress() {
            $('#txtPropertyAddress').val($('#txtPropertyStreet').val() + ' ' + $('#txtPropertySuburb').val() + ' ' + $('#txtPropertyState').val() + ' ' + $('#txtPropertyPostCode').val());
            $('#dlgPropertyAddress').dialog('close');
        }

        function setVehicleOwnerFullName() {
            $('#txtVehicleOwner').val($('#txtVehicleOwnerFirstName').val() + ' ' + $('#txtVehicleOwnerMiddleName').val() + ' ' + $('#txtVehicleOwnerSurName').val());
            $('#dlgVehicleOwnerName').dialog('close');
        }

        function defaultFormatDate(value) {
            if (value == '')
                return value;
            dArr = value.split('/');
            return dArr[1] + '/' + dArr[0] + '/' + dArr[2];
        }
        function formatDate(value, row) {
            if (value == '0001-01-01T00:00:00' || value == null || value == '')
                return '';
            var date = new Date(value);
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();

            return (d < 10 ? ('0' + d) : d) + '/' + (m < 10 ? ('0' + m) : m) + '/' + y;
        }
        function newProperty() {
            $('#dlgProperty').dialog('open');
            resetControl();
        }
        function newVehicle() {
            $('#dlgVehicle').dialog('open');
            resetControl();
        }
        function newCompany() {
            $('#dlgCompany').dialog('open');
            resetControl();
        }

        function saveVehicleSecuity() {
            var lnid = getQuerystring('LoanID', '');
            var id;
            if ($('#hdnVehicleSecurityID').val() == '')
                id = 0;
            else
                id = $('#hdnVehicleSecurityID').val();
            $.ajax({
                url: '/WebServices/AM/Security.ashx?method=SaveVehicleSecurity',
                contentType: "application/json; charset=utf-8",
                data: {
                    SecurityVehicleID: id, VIN: $('#txtVIN').val(), EngineNo: $('#txtEngineNo').val(), VehicleRegNo: $('#txtVehicleRegNo').val(), VehicleColour: $('#txtVehicleColour').val(), VehicleBadge: $('#txtVehicleBadge').val(),
                    VehicleModel: $('#txtVehicleModel').val(), VehicleMake: $('#txtVehicleMake').val(), VehicleYear: $('#txtVehicleYear').numberbox('getValue'), RegisteredOwner: $('#txtVehicleOwner').val(),
                    VehicleSecurityStatus: $('#cmbVehicleStatus').combobox('getValue'), VehicleValue: $('#txtVehicleValue').numberbox('getValue'), VehicleOwning: $('#txtVehicleOwning').numberbox('getValue'),
                    VehicleValueSource: $('#txtVehicleValueSource').val(), VehicleOwningSource: $('#txtVehicleOwningSource').val(), ExpiryDate: defaultFormatDate($('#txtExpiryDate').datebox('getValue')),
                    InsuranceType: $('#txtInsuranceType').val(), VehicleComment: $('#txtVehicleComment').val(), LoanID: lnid
                },
                success: function (data) {
                    if (id == 0) {
                        alert('New Vehicle Security Added Successfully');
                        var rows = $('#dgSecurity').datagrid('getRows');
                        countTotalSecurity(rows.length + 1);
                    }
                    else
                        alert('Vehicle Security Updated Successfully');
                    $('#dgSecurity').datagrid('reload');
                    $('#dlgVehicle').dialog('close');
                }
            });
        }

        function SavePropertySecurity() {
            var lnid = getQuerystring('LoanID', '');
            var id;
            if ($('#hdnPropertySecurityID').val() == '')
                id = 0;
            else
                id = $('#hdnPropertySecurityID').val();
            $.ajax({
                url: '/WebServices/AM/Security.ashx?method=savePropertySecurity',
                contentType: "application/json; charset=utf-8",
                data: {
                    SecurityPropertyID: id, Address: $('#txtPropertyAddress').val(), TitleRef: $('#txtTitleRef').val(), PropertyType: $('#txtPropertyType').val(),
                    PropertyOwner: $('#txtPropertyOwner').val(), PropertySecurityStatus: $('#cmbPropertyStatus').combobox('getValue'), InterestType: $('#txtInterestType').val(),
                    LodgmentDate: defaultFormatDate($('#txtLodgementDate').datebox('getValue')), CompletionDate: defaultFormatDate($('#txtCompletionDate').datebox('getValue')), PropertyValue: $('#txtPropertyValue').numberbox('getValue'),
                    PropertyOwning: $('#txtPropertyOwning').numberbox('getValue'), PropertyValueSource: $('#txtPropertyValueSource').val(), PropertyOwningSource: $('#txtPropertyOwningSource').val(),
                    Mortgagee: $('#txtMortgagee').val(), SubSequentParties: $('#txtSubParties').val(), PropertyComment: $('#txtPropertyComment').val(), LoanID: lnid
                },
                success: function (data) {
                    resetControl();
                    $('#dgSecurity').datagrid('reload');
                    if (id == 0) {
                        alert('New Property Security Added Successfully');
                        var rows = $('#dgSecurity').datagrid('getRows');
                        countTotalSecurity(rows.length + 1);
                    }
                    else
                        alert('Property Security Updated Successfully');
                    $('#dlgProperty').dialog('close');
                }
            });
        }

        function SaveCompanySecurity() {
            var lnid = getQuerystring('LoanID', '');
            var id;
            if ($('#hdnCompanySecurityID').val() == '')
                id = 0;
            else
                id = $('#hdnCompanySecurityID').val();
            $.ajax({
                url: '/WebServices/AM/Security.ashx?method=saveCompanySecurity',
                contentType: "application/json; charset=utf-8",
                data: {
                    SecurityCompanyID: id, Company: $('#txtCompany').val(), Security: $('#txtSecurity').val(), SecurityStatus: $('#txtSecurityStatus').combobox('getValue'),
                    RegistrationDate: defaultFormatDate($('#txtRegistrationDate').datebox('getValue')), PriorChargee: $('#txtPriorChargee').numberbox('getValue'),
                    SubsequentChargee: $('#txtSubsequentChargee').numberbox('getValue'), Comment: $('#txtComment').val(), LoanID: lnid
                },
                success: function (data) {
                    resetControl();
                    $('#dgSecurity').datagrid('reload');
                    if (id == 0) {
                        alert('New Company Security Added Successfully');
                        var rows = $('#dgSecurity').datagrid('getRows');
                        countTotalSecurity(rows.length + 1);
                    }
                    else
                        alert('Company Security Updated Successfully');
                    $('#dlgCompany').dialog('close');
                }
            });
        }

        function countTotalSecurity(rowcount) {
            var tab = $('#tabSummary').tabs('getSelected');
            $('#tabSummary').tabs('update', {
                tab: tab,
                options: {
                    title: 'Security(' + rowcount + ')'
                }
            });
        }
        function resetControl() {
            $('#txtPropertyAddress').val('');
            $('#txtTitleRef').val('');
            $('#txtPropertyType').val('');
            $('#txtPropertyOwner').val('');
            $('#txtInterestType').val('');
            $('#txtLodgementDate').datebox('setValue', '');
            $('#txtCompletionDate').datebox('setValue', '');
            $('#txtPropertyValue').numberbox('setValue', '');
            $('#txtPropertyOwning').numberbox('setValue', '');
            $('#txtPropertyValueSource').val('');
            $('#txtPropertyOwningSource').val('');
            $('#txtMortgagee').val('');
            $('#txtSubParties').val('');
            $('#txtPropertyComment').val('');
            $('#hdnPropertySecurityID').val('');

            $('#hdnVehicleSecurityID').val('');
            $('#txtEngineNo').val('');
            $('#txtVehicleRegNo').val('');
            $('#txtVehicleColour').val('');
            $('#txtVehicleBadge').val('');
            $('#txtVehicleModel').val('');
            $('#txtVehicleMake').val('');

            $('#txtVIN').val('');
            $('#txtVehicleYear').numberbox('setValue', '');
            $('#txtVehicleOwner').val('');
            $('#cmbVehicleStatus').combobox('setValue', '');
            $('#txtVehicleValue').numberbox('setValue', '');
            $('#txtVehicleOwning').numberbox('setValue', '');
            $('#txtVehicleValueSource').val('');
            $('#txtVehicleOwningSource').val('');
            $('#txtExpiryDate').datebox('setValue', '');
            $('#txtInsuranceType').val('');
            $('#txtVehicleComment').val('');

            $('#txtCompany').val('');
            $('#txtSecurity').val('');
            $('#txtSecurityStatus').combobox('setValue', '');
            $('#txtRegistrationDate').datebox('setValue', '');
            $('#txtPriorChargee').numberbox('setValue', '');
            $('#txtSubsequentChargee').numberbox('setValue', '');
            $('#txtComment').val('');
            $('#hdnCompanySecurityID').val('');
        }

        function deleteSecurity() {
            var row = $('#dgSecurity').datagrid('getSelected');
            if (row) {
                $.ajax({
                    url: '/WebServices/AM/Security.ashx?method=DeleteSecurityByID',
                    contentType: "application/json; charset=utf-8",
                    data: { securityid: row.id, type: row.SecurityType },
                    success: function () {
                        $('#dgSecurity').datagrid('reload');
                        var rows = $('#dgSecurity').datagrid('getRows');
                        alert('Security Deleted Successfully  ');
                        countTotalSecurity(rows.length - 1);
                    }
                });
            }
        }


        function editSecurity() {
            var row = $('#dgSecurity').datagrid('getSelected');
            var securityType = row.SecurityType;

            $.ajax({
                url: '/WebServices/AM/Security.ashx?method=GetSecurityById',
                contentType: "application/json; charset=utf-8",
                data: { securityid: row.id, type: securityType },
                success: function (data) {
                    $.each(eval(data.replace(/[\r\n]/, '')), function (i, rec) {
                        if (securityType == 'Property') {
                            $('#hdnPropertySecurityID').val(rec.SecurityPropertyID);

                            $('#txtPropertyAddress').val(rec.Address);
                            var SplitPropertyAddress = (rec.Address).split(" ");
                            $('#txtPropertyStreet').val(SplitPropertyAddress[0]);
                            $('#txtPropertySuburb').val(SplitPropertyAddress[1]);
                            $('#txtPropertyState').val(SplitPropertyAddress[2]);
                            $('#txtPropertyPostCode').val(SplitPropertyAddress[3]);
                            $('#txtTitleRef').val(rec.TitleRef);
                            $('#txtPropertyType').val(rec.PropertyType);
                            $('#txtPropertyOwner').val(rec.PropertyOwner);
                            var SplitPropertyOwner = (rec.PropertyOwner).split(" ");
                            $('#txtOwnerFirstName').val(SplitPropertyOwner[0]);
                            $('#txtOwnerMiddleName').val(SplitPropertyOwner[1]);
                            $('#txtOwnerSurName').val(SplitPropertyOwner[2]);
                            $('#txtInterestType').val(rec.InterestType);
                            $('#txtLodgementDate').datebox('setValue', formatDate(rec.LodgmentDate, null));
                            $('#txtCompletionDate').datebox('setValue', formatDate(rec.CompletionDate, null));
                            $('#txtPropertyValue').numberbox('setValue', rec.PropertyValue);
                            $('#txtPropertyOwning').numberbox('setValue', rec.PropertyOwning);
                            $('#txtPropertyValueSource').val(rec.PropertyValueSource);
                            $('#txtPropertyOwningSource').val(rec.PropertyOwningSource);
                            $('#txtMortgagee').val(rec.Mortgagee);
                            $('#txtSubParties').val(rec.SubSequentParties);
                            $('#txtPropertyComment').val(rec.PropertyComment);
                            $('#dlgProperty').dialog('open');
                            $('#dlgVehicle').dialog('close');
                            $('#dlgCompany').dialog('close');
                        }
                        else if (securityType == 'Vehicle') {
                            $('#hdnVehicleSecurityID').val(rec.SecurityVehicleID);
                            $('#txtEngineNo').val(rec.EngineNo);
                            $('#txtVehicleRegNo').val(rec.VehicleRegNo);
                            $('#txtVehicleColour').val(rec.VehicleColour);
                            $('#txtVehicleBadge').val(rec.VehicleBadge);
                            $('#txtVehicleModel').val(rec.VehicleModel);
                            $('#txtVehicleMake').val(rec.VehicleMake);

                            $('#txtVIN').val(rec.VIN);
                            $('#txtVehicleYear').numberbox('setValue', rec.VehicleYear);
                            $('#txtVehicleOwner').val(rec.RegisteredOwner);
                            var SplitVehiclePropertyOwner = (rec.RegisteredOwner).split(" ");
                            $('#txtVehicleOwnerFirstName').val(SplitVehiclePropertyOwner[0]);
                            $('#txtVehicleOwnerMiddleName').val(SplitVehiclePropertyOwner[1]);
                            $('#txtVehicleOwnerSurName').val(SplitVehiclePropertyOwner[2]);

                            $('#cmbVehicleStatus').combobox('setValue', rec.VehicleSecurityStatus);
                            $('#txtVehicleValue').numberbox('setValue', rec.VehicleValue);
                            $('#txtVehicleOwning').numberbox('setValue', rec.VehicleOwning);
                            $('#txtVehicleValueSource').val(rec.VehicleValueSource);
                            $('#txtVehicleOwningSource').val(rec.VehicleOwningSource);

                            $('#txtExpiryDate').datebox('setValue', formatDate(rec.ExpiryDate, null));
                            $('#txtInsuranceType').val(rec.InsuranceType);
                            $('#txtVehicleComment').val(rec.VehicleComment);
                            $('#dlgVehicle').dialog('open');
                            $('#dlgProperty').dialog('close');
                            $('#dlgCompany').dialog('close');
                        }
                        else if (securityType == 'Company') {
                            $('#hdnCompanySecurityID').val(rec.companyid);
                            $('#txtCompany').val(rec.CompanyName);
                            $('#txtSecurity').val(rec.CompanySecurity);
                            $('#txtSecurityStatus').combobox('setValue', rec.SecurityStatus);
                            $('#txtRegistrationDate').datebox('setValue', formatDate(rec.RegistrationDate, null));
                            $('#txtPriorChargee').numberbox('setValue', rec.PriorChargee);
                            $('#txtSubsequentChargee').numberbox('setValue', rec.SubsequentChargee);
                            $('#txtComment').val(rec.CompanyComment);
                            $('#dlgCompany').dialog('open');
                            $('#dlgVehicle').dialog('close');
                            $('#dlgProperty').dialog('close');
                        }
                    });
                }
            });
        }

        function UpdateComment() {
            var recid = 0;
            if ($('#hdnType').val() == 'Property') {
                recid = $('#hdnPropertySecurityID').val();
                $.ajax({
                    url: '/WebServices/AM/Security.ashx?method=UpdatePropertyComment',
                    contentType: "application/json; charset=utf-8",
                    data: {
                        Propertyid: recid,
                        PropertyComment: $('#lblPropertyComment').val()
                    },
                    success: function (data) {
                        alert('Record updated Successfully');
                    }
                });
            }
            else if ($('#hdnType').val() == 'Vehicle') {
                recid = $('#hdnVehicleSecurityID').val();
                $.ajax({
                    url: '/WebServices/AM/Security.ashx?method=UpdateVehicleComment',
                    contentType: "application/json; charset=utf-8",
                    data: {
                        Vehicleid: recid,
                        VehicleComment: $('#lblVehicleComment').val()
                    },
                    success: function (data) {
                        alert('Record updated Successfully');
                    }
                });
            }
            else if ($('#hdnType').val() == 'Company') {
                recid = $('#hdnCompanySecurityID').val();
                $.ajax({
                    url: '/WebServices/AM/Security.ashx?method=UpdateCompanyComment',
                    contentType: "application/json; charset=utf-8",
                    data: {
                        Companyid: recid,
                        CompanyComment: $('#txtCompanyComment').val()
                    },
                    success: function (data) {
                        alert('Record updated Successfully');
                    }
                });
            }
        }
        function showDetail() {
            var row = $('#dgSecurity').datagrid('getSelected');
            var securityType = row.SecurityType;
            $('#hdnType').val(securityType);
            $.ajax({
                url: '/WebServices/AM/Security.ashx?method=GetSecurityById',
                contentType: "application/json; charset=utf-8",
                data: { securityid: row.id, type: securityType },
                success: function (data) {
                    $.each(eval(data.replace(/[\r\n]/, '')), function (i, rec) {
                        if (securityType == 'Property') {
                            $('#hdnPropertySecurityID').val(rec.SecurityPropertyID);
                            $('#hdnVehicleSecurityID').val('');

                            $('#lblAddress').text(rec.Address);
                            $('#lblTitleRef').text(rec.TitleRef);
                            $('#lblPropertyType').text(rec.PropertyType);
                            $('#lblPropertyOwner').text(rec.PropertyOwner);
                            $('#lblInterestType').text(rec.InterestType);

                            $('#lblLodgementDate').text(formatDate(rec.LodgmentDate, null));

                            $('#lblCompletionDate').text(formatDate(rec.CompletionDate, null));

                            $('#lblPropertyStatus').text(rec.PropertySecurityStatus);
                            $('#lblPropertyValue').text(rec.PropertyValue);
                            $('#lblPropertyOwning').text(rec.PropertyOwning);
                            $('#lblPropertyValueSource').text(rec.PropertyValueSource);
                            $('#lblPropertyOwningSource').text(rec.PropertyOwningSource);
                            $('#lblMortgagee').text(rec.Mortgagee);
                            $('#lblSubParties').text(rec.SubSequentParties);
                            $('#lblPropertyComment').val(rec.PropertyComment);

                            $('#PropertySecurityDetail').show();
                            $('#VehicleSecurityDetail').hide();
                            $('#CompanySecurityDetail').hide();
                        }
                        else if (securityType == 'Vehicle') {
                            $('#hdnVehicleSecurityID').val(rec.SecurityVehicleID);
                            $('#hdnPropertySecurityID').val('');
                            $('#VehicleSecurityDetail').show();
                            $('#PropertySecurityDetail').hide();
                            $('#CompanySecurityDetail').hide();
                            $('#lblVIN').text(rec.VIN);
                            $('#lblEngineNo').text(rec.EngineNo);
                            $('#lblRegNo').text(rec.VehicleRegNo);
                            $('#lblVehicleColour').text(rec.VehicleColour);
                            $('#lblVehicleBadge').text(rec.VehicleBadge);
                            $('#lblVehicleModel').text(rec.VehicleModel);
                            $('#lblVehicleMake').text(rec.VehicleMake);
                            $('#lblVehicleYear').text(rec.VehicleYear);
                            $('#lblVehicleOwner').text(rec.RegisteredOwner);
                            $('#lblVehicleStatus').text(rec.VehicleSecurityStatus);
                            $('#lblVehicleValue').text(rec.VehicleValue);
                            $('#lblVehicleOwning').text(rec.VehicleOwning);
                            $('#lblVehicleValueSource').text(rec.VehicleValueSource);
                            $('#lblVehicleOwningSource').text(rec.VehicleOwningSource);

                            $('#lblExpiryDate').text(formatDate(rec.ExpiryDate, null));
                            $('#lblInsuranceType').text(rec.InsuranceType);
                            $('#lblVehicleComment').val(rec.VehicleComment);

                            //alert($('#lblVIN').val() + ' ' + ' ' + ' ' + $('#lblEngineNo').val() + ' ' + ' ' + ' ' + $('#lblRegNo').val() + ' ' + ' ' + ' ' + $('#lblVehicleColour').val() + ' ' + ' ' + ' ' + $('#lblVehicleBadge').val() + ' ' + ' ' + ' ' + $('#lblVehicleModel').val() + ' ' + ' ' + ' ' + $('#lblVehicleMake').val() + ' ' + ' ' + ' ' + $('#lblVehicleYear').val() + ' ' + ' ' + ' ' + $('#lblVehicleOwner').val() + ' ' + ' ' + ' ' + $('#lblVehicleStatus').val() + ' ' + $('#lblVehicleValue').val() + ' ' + $('#lblVehicleOwning').val() + ' ' + $('#lblVehicleValueSource').val() + ' ' + $('#lblVehicleOwningSource').val() + ' ' + $('#lblExpiryDate').val() + ' ' + $('#lblInsuranceType').val());
                        }
                        else if (securityType == 'Company') {
                            $('#hdnCompanySecurityID').val(rec.companyid);
                            $('#lblCompany').text(rec.CompanyName);
                            $('#lblCompanySecurity').text(rec.CompanySecurity);
                            $('#lblCompanySecurityStatus').text(rec.SecurityStatus);
                            $('#lblRegistrationDate').text(formatDate(rec.RegistrationDate, null));
                            $('#lblPriorChargee').text(rec.PriorChargee);
                            $('#lblSubsequentChargee').text(rec.SubsequentChargee);
                            $('#txtCompanyComment').val(rec.CompanyComment);
                            $('#CompanySecurityDetail').show();
                            $('#PropertySecurityDetail').hide();
                            $('#VehicleSecurityDetail').hide();
                        }
                    });
                }
            });
        }

    </script>
</body>
</html>